const {
  TestCaseTransformer,
  STUB_KIND,
  VARIABLE_KIND,
} = require('smart-rocket-unit');
class YmlTestCaseTransformer extends TestCaseTransformer {
  /**
   * 构造函数
   * @param {Object} testCaseData 测试用例数据
   * @param {Object} variablesData 函数变量信息数据
   * @param {Object} typeSystemData 类型系统数据
   */
  constructor(testCaseData, variablesData, typeSystemData) {
    super(testCaseData, variablesData, typeSystemData);
    this.startups = []; // 初始化信息（指针目标数据）
    this.variables = []; // 变量信息（全局变量、形参）
    this.stubs = []; // 桩信息
    this.globals = []; // 全局变量数据
    this.globalStubs = []; // 桩数据
  }

  transform() {
    super.transform();
  }

  /**
   * 构建桩函数数据包含次数
   * @param {*} stubName
   */
  buildStubFields(stubName) {
    if (!this.testCaseData.data.stubs[stubName]) {
      this.testCaseData.data.stubs[stubName] = { '@value': '' };
    }
    const stub = this.variablesData.stubs[stubName];
    this.testCaseData.data.stubs[stubName]['@type'] = stub;
    const returnTypeName = stub['%'] ? stub['%']['@type'] : 'void';
    const times = [];
    let currentTimes = this.testCaseData.data.stubs[stubName].times;
    if (!currentTimes && currentTimes !== 0) {
      currentTimes = stub.times;
    }
    if (!currentTimes) {
      currentTimes = 0;
    }
    const result = {
      fieldPath: `stubs.${stubName}`,
      kind: VARIABLE_KIND.stub,
      name: TestCaseTransformer.formatStubFieldName(returnTypeName, stub['@name'] || stubName, stub.params, stub['@scopes']),
      stubName,
      typeName: returnTypeName,
      times: [],
      time: 0,
      currentTimes,
      currentStubKind: this.testCaseData.data.stubs[stubName].kind || STUB_KIND.value,
      children: [],
      data: stub,
    };
    for (let i = 0; i <= currentTimes; i += 1) {
      times.push(i);
      result.children.push([]);
    }
    result.times = times;
    this.stubsData.push(result);
    if (result.currentStubKind === STUB_KIND.code) {
      return;
    }
    for (let i = 0; i < times.length; i += 1) {
      this.addStubOutputFields(stubName, parseInt(times[i], 10), result.children[i]);
      if (this.testCaseData.data.stubs[stubName]['@value']) {
        let templateData = [];
        Object.keys(this.testCaseData.data.stubs[stubName]['@value']).forEach((time) => {
          this.addStubOutputFields(stubName, parseInt(time, 10), templateData);
        });
        templateData = [];
      }
    }
  }
}
module.exports = YmlTestCaseTransformer;
